const express=require('express')

//路由
const router=express.Router()

router.get('/test',(req,res)=>{
	res.send('测试成功')
})

//加载首页分类数据-----滑块
const indexData=require('../datas/index.json')
router.get('/getIndexData',(req,res)=>{
	res.send({
		status:200,
		indexData
	})
})


//加载首页分类列表内容
const indexCateList=require('../datas/indexCateList.json')
router.get('/getIndexCateList',(req,res)=>{
	res.send({
		status:200,
		indexCateList
	})
})

//tabbar	分类
const categoryDatas=require('../datas/categoryDatas.json')
router.get('/getCategoryDatas',(req,res)=>{
	res.send({
		status:200,
		categoryDatas
	})
})

//连接本地数据库
const userInfo = require('../db/sql.js')
//读取数据并放在user中
router.get('/getUserInfo', function(req,res,next){
	userInfo.query('SELECT * FROM userofstore',function(error,userInfo,fields){
		if(error){
			throw error;
		}
		res.send({
			status:200,
			userInfo
		})
	});
});
//充值功能
router.get('/recharge',(req,res)=>{
	/*let name=req.query.name;
	let age=req.query.age;
	res.send('Hello '+name+','+age);*/
	var account=req.query.account;
	var nickName=req.query.nickName;
	userInfo.query('UPDATE userofstore SET balance=? WHERE nickName=?',[account,nickName],function(error,userInfo,fields){
		if(error){
			throw error;
		}
		res.send({
			status:200,
			resault:'充值成功'
		})
	})
})
//下单
router.get('/buy',(req,res)=>{
	var account=req.query.account;
	var nickName=req.query.nickName;
	userInfo.query('UPDATE userofstore SET balance=? WHERE nickName=?',[account,nickName],function(error,userInfo,fields){
		if(error){
			throw error;
		}
		res.send({
			status:200,
			resault:'消费成功'
		})
	})
})
//新增用户
router.get('/register',(req,res)=>{
	var nickname=req.query.nickName;
	var username=req.query.userName;
	var phonenumber=req.query.phoneNumber;
	var age=req.query.age;
	var pwd=req.query.pwd;
	var gender=req.query.gender;
	userInfo.query('INSERT INTO userofstore(nickName,userName,phoneNum,age,pwd,gender,balance) VALUES(?,?,?,?,?,?,0);',[nickname,username,phonenumber,age,pwd,gender],
	function(error,userInfo,fields){
		if(error){
			throw error;
		}
		res.send({
			status:200,
			resault:'注册成功'
		})
	})
})
//注销用户
router.get('/logout',(req,res)=>{
	var nickname=req.query.nickName;
	var pwd=req.query.pwd;
	userInfo.query('DELETE FROM userofstore WHERE nickName=? AND pwd=?;',[nickname,pwd],
	function(error,userInfo,fields){
		if(error){
			throw error;
		}
		res.send({
			status:200,
			resault:'注销成功'
		})
	})
})
//更改用户名
router.get('/updateUsername',(req,res)=>{
	var newname=req.query.username;
	var oldname=req.query.oldname;
	userInfo.query('UPDATE userofstore SET nickName=? where nickName=?',[newname,oldname],
	function(error,userInfo,fields){
		if(error){
			throw error;
		}
		res.send({
			status:200,
			resault:'修改成功'
		})
	})
})
//更改密码
router.get('/updatePwd',(req,res)=>{
	var pwd=req.query.pwd;
	var nickname=req.query.nickName;
	userInfo.query('UPDATE userofstore SET pwd=? where nickName=?',[pwd,nickname],
	function(error,userInfo,fields){
		if(error){
			throw error;
		}
		res.send({
			status:200,
			resault:'修改成功'
		})
	})
})
//更改电话号
router.get('/updatePhonenum',(req,res)=>{
	var num=req.query.num;
	var nickname=req.query.nickName;
	userInfo.query('UPDATE userofstore SET phoneNum=? where nickName=?',[num,nickname],
	function(error,userInfo,fields){
		if(error){
			throw error;
		}
		res.send({
			status:200,
			resault:'修改成功'
		})
	})
})

module.exports=router;